package tech.xs.auth.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import tech.xs.auth.api.domain.bo.password.CurrUserModifyPasswordBo;
import tech.xs.auth.domain.bo.password.ImageVerifyCodeBo;
import tech.xs.auth.domain.bo.password.InitPasswordBo;
import tech.xs.auth.domain.bo.password.PasswordCertificationBo;
import tech.xs.auth.domain.bo.password.PasswordLoginBo;
import tech.xs.auth.api.domain.bo.password.ResetDefaultPasswordBo;
import tech.xs.auth.service.AuthPasswordService;
import tech.xs.common.auth.domain.cache.AuthTokenCache;
import tech.xs.common.auth.domain.enums.AuthPathWhiteTypeEnum;
import tech.xs.common.framework.domain.annotations.AuthApi;
import tech.xs.common.framework.domain.model.ApiResult;
import tech.xs.common.sys.domain.constant.PermissionConstant;

import javax.annotation.Resource;

@Slf4j
@Validated
@RestController
@RequestMapping("/password")
public class PasswordController {

    @Resource
    private AuthPasswordService passwordService;

    @PostMapping("/login")
    @AuthApi(type = AuthPathWhiteTypeEnum.ALL)
    public ApiResult<AuthTokenCache> login(@RequestBody PasswordLoginBo bo) {
        return ApiResult.success(passwordService.loginByUserName(bo));
    }

    @GetMapping("/certification")
    @AuthApi(type = AuthPathWhiteTypeEnum.ALL)
    public ApiResult<PasswordCertificationBo> getCertification() {
        return ApiResult.success(passwordService.getCertification());
    }

    @GetMapping("/imageVerifyCode")
    @AuthApi(type = AuthPathWhiteTypeEnum.ALL)
    public ApiResult<ImageVerifyCodeBo> getImageVerifyCode() {
        return ApiResult.success(passwordService.getImageVerifyCode());
    }

    @PostMapping("/initUserPassword")
    @AuthApi(type = AuthPathWhiteTypeEnum.ALL)
    public ApiResult<Void> initUserPassword(@RequestBody InitPasswordBo bo) {
        passwordService.initUserPassword(bo);
        return ApiResult.success();
    }

    @PostMapping("/resetDefaultPassword")
    @AuthApi(permissions = PermissionConstant.USER_EDIT)
    public ApiResult<Void> resetDefaultPassword(@RequestBody ResetDefaultPasswordBo bo) {
        passwordService.resetDefaultPassword(bo.getUserName());
        return ApiResult.success();
    }

    /**
     * 当前登陆用户通过原密码修改密码
     */
    @PostMapping("/currUserModifyPassword")
    @AuthApi(type = AuthPathWhiteTypeEnum.LOGIN)
    public ApiResult<Void> currUserModifyPassword(@RequestBody CurrUserModifyPasswordBo bo) {
        passwordService.currUserModifyPassword(bo);
        return ApiResult.success();
    }

}

